package com.hsy.linemoney.modules.home;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.ServiceTypeService;

import java.util.List;

/**
 * Created by MartinKent on 2016/9/12.
 */
public class ServiceTypeListFragment extends Fragment {
    private ListView listView;
    private List<ServiceTypeService.ServiceTypeListResult.Value> data;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        listView = new ListView(inflater.getContext());
        listView.setDivider(new ColorDrawable(Color.TRANSPARENT));
        listView.setDividerHeight(0);
        listView.setCacheColorHint(Color.TRANSPARENT);
        listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        listView.setAdapter(new MyAdapter());
        return listView;
    }

    public void setData(List<ServiceTypeService.ServiceTypeListResult.Value> data) {
        this.data = data;
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return null == data ? 0 : data.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (null == convertView) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.service_typr_list_item, null);
                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.amount = (TextView) convertView.findViewById(R.id.price);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.name.setText(data.get(position).getServiceName());
            holder.amount.setText("$" + data.get(position).getAmount());
            return convertView;
        }

        private class ViewHolder {
            public TextView name;
            public TextView amount;
        }
    }
}
